<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
          <span style="font-weight: bold;color: #0099ff;font-size: 16px;">筛选条件</span>
      </div> 
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px" label-position="top">
        <el-form-item label="客户名称" prop="khName">
          <el-input v-model="queryParams.khName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        
        <el-form-item label="国家地区" prop="khCountry">
          <el-select v-model="queryParams.khCountry" filterable>
            <el-option v-for="item in countries" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="省份" prop="khProvince">
          <el-select v-model="queryParams.khProvince" filterable @change="changeProvince">
            <el-option v-for="item in provinces" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="khCity">
          <el-select v-model="queryParams.khCity" filterable>
            <el-option v-for="item in cities" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机" prop="khHandset">
          <el-input
            v-model="queryParams.khHandset"
            placeholder="请输入手机"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="来源" prop="khFrom">
          <el-select v-model="queryParams.khFrom" filterable>
            <el-option v-for="item in sources" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="box-card" style="margin-top: 5px;">
      <div slot="header" class="clearfix">
          <span style="font-weight: bold;color: #0099ff;font-size: 16px;">查询列表</span>
      </div> 
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="SysCrmKhglList" border stripe height="550">
        <el-table-column label="客户名称" align="left" prop="khName" fixed width="400">
          <template slot-scope="scope">
            <span style="color:black;font-size: 16px;">{{ scope.row.khName }}</span><span style="font-size: 12px;color: blue;">({{ scope.row.khHandset }})</span>
          </template>
        </el-table-column>
        <el-table-column label="国家地区" align="left" prop="khCountry" />
        <el-table-column label="省份" align="left" width="140" prop="khProvince" />
        <el-table-column label="城市" align="left" prop="khCity" />
        <el-table-column label="客户类型" align="center" prop="khType" width="200" />
        <el-table-column label="客户来源" align="center" prop="khFrom" width="150" />
        <el-table-column label="所属员工" align="center" width="150" prop="dataUserName" />
        <el-table-column label="创建时间" align="center" prop="addtime" width="180" />
        <el-table-column label="购买品种" align="left" prop="diyCbox1" width="300" />
        <el-table-column label="片区" align="center" prop="diyCbox2" />
        <el-table-column label="操作" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-document"
              @click="handleUpdate(scope.row)"
            >查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { listSysCrmKhgl, getSysCrmKhgl, delSysCrmKhgl, addSysCrmKhgl, updateSysCrmKhgl } from "@/api/system/customer/khgl";
import { getInitParams,getCities } from "@/api/system/customer/khgl";
export default {
  name: "Khgl",
  data() {
    return {
      countries: [],
      provinces: [],
      cities: [],
      sources: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      SysCrmKhglList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        khName: null,
        khCountry: null,
        khProvince: null,
        khCity: null,
        khHandset: null,
        khFrom: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getInitParams();
    this.queryParams.khCountry = "中国";
    this.queryParams.khProvince = "all";
    this.queryParams.khCity = "all";
    this.queryParams.khFrom = "all";
  },
  methods: {
    changeProvince(){
      if(this.queryParams.khProvince=="all"){
        this.cities = [];
        this.cities = [{
          value:"all",
          label:"(全部)"
        }];
      }else{
        let data = {
          khProvince:this.queryParams.khProvince
        }
        getCities(data).then(res =>{
          this.cities = res.data.cities;
        })
      }
      this.queryParams.khCity = "all";
    },
    getInitParams(){
      getInitParams().then(res =>{
        this.countries = res.data.countries;
        this.provinces = res.data.provinces;
        this.sources = res.data.sources;
        this.cities = res.data.cities;
      })
    },
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      listSysCrmKhgl(this.queryParams).then(response => {
        this.SysCrmKhglList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        khId: null,
        khClass: null,
        khName: null,
        khShortname: null,
        khAddress: null,
        khCountry: null,
        khProvince: null,
        khCity: null,
        khCoaddress: null,
        khHottype: null,
        khHotlevel: null,
        khType: null,
        khStatus: null,
        khSn: null,
        khHandset: null,
        khFrom: null,
        khCreditgrade: null,
        khValrating: null,
        khRemark: null,
        dataUserid: null,
        dataUserName: null,
        deptid: null,
        khYopuserid: null,
        khYopuserName: null,
        khYopuserDeptid: null,
        addtime: null,
        khFenpeitime: null,
        khGenzongtime: null,
        khMarkclass: null,
        diyCbox1: null,
        diyCbox2: null,
        diyDdl2: null,
        diyDdl3: null,
        khIndustry: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.khId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const khId = row.khId || this.ids
      getSysCrmKhgl(khId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.khId != null) {
            updateSysCrmKhgl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysCrmKhgl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const khIds = row.khId || this.ids;
      this.$modal.confirm('是否确认删除客户信息编号为"' + khIds + '"的数据项？').then(function() {
        return delSysCrmKhgl(khIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/crmkhgl/export', {
        ...this.queryParams
      }, `客户信息数据_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
